/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ez2find.controllers;

import com.ez2find.backing.CommunityBean;
import com.ez2find.backing.EventBean;
import com.ez2find.backing.GigBean;
import com.ez2find.backing.HousBean;
import com.ez2find.backing.JobBean;
import com.ez2find.backing.PersonalBean;
import com.ez2find.backing.ResumeBean;
import com.ez2find.backing.SaleBean;
import com.ez2find.backing.ServiceBean;
import com.ez2find.manager.PostManagerLocal;
import com.ez2find.manager.SiteManagerLocal;
import com.ez2find.model.Community;
import com.ez2find.model.Event;
import com.ez2find.model.Gig;
import com.ez2find.model.Hous;
import com.ez2find.model.Job;
import com.ez2find.model.Personal;
import com.ez2find.model.Post;
import com.ez2find.model.Resume;
import com.ez2find.model.Sale;
import com.ez2find.model.Service;
import com.ez2find.util.SessionContext;
import java.io.*;
import java.net.*;

import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Owner
 */
public class Posting extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    @EJB
    private PostManagerLocal ejbPostManager;
    @EJB
    private SiteManagerLocal ejbSiteManager;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if(request.getParameter("site")!=null && request.getParameter("id")!=null){
        Post post = ejbPostManager.getPostingFound(ejbSiteManager.find(Long.parseLong(request.getParameter("site"))), request.getParameter("type"), Long.parseLong(request.getParameter("id")));
        if (post != null) {
            if (post.getType().equals("job")) {
                JobBean jobBean = (JobBean) SessionContext.getAttribute("jobBean");
                if (jobBean != null) {
                    jobBean.setJob((Job) post);
                    jobBean.setBack(0);
                } else {
                    JobBean newJobBean = new JobBean();
                    newJobBean.setJob((Job) post);
                    SessionContext.setAttribute("jobBean", newJobBean);
                }
                getServletContext().getRequestDispatcher("/jobPosting/view.jsp").forward(request, response);
            }
            if (post.getType().equals("resume")) {
                ResumeBean resumeBean = (ResumeBean) SessionContext.getAttribute("resumeBean");
                if (resumeBean != null) {
                    resumeBean.setResume((Resume) post);
                    resumeBean.setBack(0);
                } else {
                    ResumeBean newResumeBean = new ResumeBean();
                    newResumeBean.setResume((Resume) post);
                    SessionContext.setAttribute("resumeBean", newResumeBean);
                }
                getServletContext().getRequestDispatcher("/resumePosting/view.jsp").forward(request, response);
            }
            if (post.getType().equals("house")) {
                HousBean housBean = (HousBean) SessionContext.getAttribute("housBean");
                if (housBean != null) {
                    housBean.setHous((Hous) post);
                    housBean.setBack(0);
                } else {
                    HousBean newHousBean = new HousBean();
                    newHousBean.setHous((Hous) post);
                    SessionContext.setAttribute("housBean", newHousBean);
                }
                getServletContext().getRequestDispatcher("/housPosting/view.jsp").forward(request, response);
            }
            if (post.getType().equals("sale")) {
                SaleBean saleBean = (SaleBean) SessionContext.getAttribute("saleBean");
                if (saleBean != null) {
                    saleBean.setSale((Sale) post);
                    saleBean.setBack(0);
                } else {
                    SaleBean newSaleBean = new SaleBean();
                    newSaleBean.setSale((Sale) post);
                    SessionContext.setAttribute("saleBean", newSaleBean);
                }
                getServletContext().getRequestDispatcher("/salePosting/view.jsp").forward(request, response);
            }
            if (post.getType().equals("gig")) {
                GigBean gigBean = (GigBean) SessionContext.getAttribute("gigBean");
                if (gigBean != null) {
                    gigBean.setGig((Gig) post);
                    gigBean.setBack(0);
                } else {
                    GigBean newGigBean = new GigBean();
                    newGigBean.setGig((Gig) post);
                    SessionContext.setAttribute("gigBean", newGigBean);
                }
                getServletContext().getRequestDispatcher("/gigPosting/view.jsp").forward(request, response);
            }
            if (post.getType().equals("service")) {
                ServiceBean serviceBean = (ServiceBean) SessionContext.getAttribute("serviceBean");
                if (serviceBean != null) {
                    serviceBean.setService((Service) post);
                    serviceBean.setBack(0);
                } else {
                    ServiceBean newServiceBean = new ServiceBean();
                    newServiceBean.setService((Service) post);
                    SessionContext.setAttribute("serviceBean", newServiceBean);
                }
                getServletContext().getRequestDispatcher("/servicePosting/view.jsp").forward(request, response);
            }
            if (post.getType().equals("community")) {
                CommunityBean communityBean = (CommunityBean) SessionContext.getAttribute("communityBean");
                if (communityBean != null) {
                    communityBean.setCommunity((Community) post);
                    communityBean.setBack(0);
                } else {
                    CommunityBean newCommunityBean = new CommunityBean();
                    newCommunityBean.setCommunity((Community) post);
                    SessionContext.setAttribute("communityBean", newCommunityBean);
                }
                getServletContext().getRequestDispatcher("/communityPosting/view.jsp").forward(request, response);
            }
            if (post.getType().equals("personal")) {
                PersonalBean personalBean = (PersonalBean) SessionContext.getAttribute("personalBean");
                if (personalBean != null) {
                    personalBean.setPersonal((Personal) post);
                    personalBean.setBack(0);
                } else {
                    PersonalBean newPersonalBean = new PersonalBean();
                    newPersonalBean.setPersonal((Personal) post);
                    SessionContext.setAttribute("personalBean", newPersonalBean);
                }
                getServletContext().getRequestDispatcher("/personalPosting/view.jsp").forward(request, response);
            }
            if (post.getType().equals("event")) {
                EventBean eventBean = (EventBean) SessionContext.getAttribute("eventBean");
                if (eventBean != null) {
                    eventBean.setEvent((Event) post);
                    eventBean.setBack(0);
                } else {
                    EventBean newEventBean = new EventBean();
                    newEventBean.setEvent((Event) post);
                    SessionContext.setAttribute("eventBean", newEventBean);
                }
                getServletContext().getRequestDispatcher("/eventPosting/view.jsp").forward(request, response);
            }
        } else {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head><title>ez2find</title></head>");
            out.println("<body>");
            out.println("<h2>Error link address is invalid, please check spelling</h2>");
            out.println("<h4>Post does not exist or the creator hasn't published it yet, or it is expired</h4><br/>");
            out.println("<a href=\"/site/faces/welcome.jsp\" >Go to eez2find.com</a>");
            out.println("</body></html>");
        }
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
